package com.hyt.it.ogt.ykcj.service.grade.impl;


import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hyt.core.util.DateUtils;
import com.hyt.it.ogt.ykcj.common.enums.CommonEnums;
import com.hyt.it.ogt.ykcj.common.exception.CustomException;
import com.hyt.it.ogt.ykcj.common.utils.IdUtils;
import com.hyt.it.ogt.ykcj.domain.grade.EnterpriseExamineeInfo;
import com.hyt.it.ogt.ykcj.domain.grade.ExamReviewInfo;
import com.hyt.it.ogt.ykcj.domain.vo.ExamReviewInfoVo;
import com.hyt.it.ogt.ykcj.mapper.grade.EnterpriseExamineeInfoMapper;
import com.hyt.it.ogt.ykcj.mapper.grade.ExamReviewInfoMapper;
import com.hyt.it.ogt.ykcj.service.grade.IEnterpriseExamineeReviewInfoService;
import com.hyt.loginfo.service.IBaseLoginService;


/**
 * 企业招聘考生复核信息Service业务层处理
 *
 * @author liying
 * @date 2021-11-19
 */
@Service
public class EnterpriseExamineeReviewInfoServiceImpl implements IEnterpriseExamineeReviewInfoService {
    private static final Logger log = LoggerFactory.getLogger(EnterpriseExamineeReviewInfoServiceImpl.class);

    @Autowired
    private ExamReviewInfoMapper examReviewInfoMapper;

    @Autowired
    private EnterpriseExamineeInfoMapper enterpriseExamineeInfoMappere;

    @Autowired
    private IBaseLoginService iBaseLoginService;

    @Override
    public ExamReviewInfoVo selectExamineeReviewInfoById(String id) {
        ExamReviewInfo examReviewInfo = examReviewInfoMapper.selectById(id);
        if(null == examReviewInfo){
            return null;
        }
        EnterpriseExamineeInfo  examineeInfo = enterpriseExamineeInfoMappere.selectEnterpriseExamineeInfoById(examReviewInfo.getExamineeId());
        ExamReviewInfoVo vo = new ExamReviewInfoVo();
        BeanUtils.copyProperties(examReviewInfo,vo);
        if(null != examineeInfo){
            vo.setReviewStatus( examineeInfo.getReviewStatus() );

        }
        if(null == vo.getReviewDay()){
            vo.setReviewDay(DateUtils.addDay( null == vo.getApplyDay() ? new Date() : vo.getApplyDay(),7));
        }
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveExamReviewInfoVo(ExamReviewInfoVo vo) throws  Exception{
        if(null == vo){
            return 0;
        }
        checkParam(vo);
        ExamReviewInfo examReviewInfo =  new ExamReviewInfo();
        BeanUtils.copyProperties(vo,examReviewInfo);
        if(Integer.valueOf(CommonEnums.REVIEW_STATUS_1.getCode()) .equals(vo.getReviewStatus()) ){
            examReviewInfo.setReviewDay(com.hyt.it.ogt.ykcj.common.utils.DateUtils.getNowDate());
        }
        if(  Integer.valueOf(CommonEnums.REVIEW_STATUS_0.getCode()).equals(vo.getReviewStatus()) ){
            examReviewInfo.setApplyDay(com.hyt.it.ogt.ykcj.common.utils.DateUtils.getNowDate());
        }
        examReviewInfo.createInfo(iBaseLoginService.getSysUser().getLoginname());
        examReviewInfo.setUpdateBy(iBaseLoginService.getSysUser().getLoginname());
        if(StringUtils.isNotEmpty(vo.getId())){
            EnterpriseExamineeInfo enterpriseExamineeInfo = new EnterpriseExamineeInfo();
            enterpriseExamineeInfo.setId(vo.getExamineeId());
            enterpriseExamineeInfo.setReviewStatus(Integer.valueOf(CommonEnums.REVIEW_STATUS_1.getCode()));
            enterpriseExamineeInfoMappere.updateEnterpriseExamineeInfo(enterpriseExamineeInfo);
            return  examReviewInfoMapper.updateById(examReviewInfo);
        }else{
            examReviewInfo.setId(IdUtils.generateId());
            return  examReviewInfoMapper.insert(examReviewInfo);
        }

     }


     private void checkParam(ExamReviewInfoVo vo) throws Exception{
         if (StringUtils.isEmpty(vo.getExamId())) {
             throw new CustomException("考试ID不能为空");
         }
         if (StringUtils.isEmpty(vo.getExamineeId())) {
             throw new CustomException("考生ID不能为空");
         }
         if (null == vo.getReviewStatus()) {
             throw new CustomException("复核状态不能为空");
         }
         if( Integer.valueOf(CommonEnums.REVIEW_STATUS_1.getCode()) .equals(vo.getReviewStatus()) ){
             if (StringUtils.isEmpty(vo.getFeedback())) {
                 throw new CustomException("请输入复核反馈信息");
             }

         }
         if(  Integer.valueOf(CommonEnums.REVIEW_STATUS_0.getCode()).equals(vo.getReviewStatus()) ){
             if (StringUtils.isEmpty(vo.getApplyReason())) {
                 throw new CustomException("申请说明不能为空");
             }

         }

     }
}
